<template>
  <el-dialog
    :title="!dataForm.auditId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="审核人" prop="auditName">
      <el-input v-model="dataForm.auditName" placeholder="审核人"></el-input>
    </el-form-item>
    <el-form-item label="0-新房  1-二手房 2-出租房" prop="auditType">
      <el-input v-model="dataForm.auditType" placeholder="0-新房  1-二手房 2-出租房"></el-input>
    </el-form-item>
    <el-form-item label="关联ID" prop="relevanceId">
      <el-input v-model="dataForm.relevanceId" placeholder="关联ID"></el-input>
    </el-form-item>
    <el-form-item label="审核状态" prop="auditStatus">
      <el-input v-model="dataForm.auditStatus" placeholder="审核状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          auditId: 0,
          auditName: '',
          auditType: '',
          relevanceId: '',
          auditStatus: ''
        },
        dataRule: {
          auditName: [
            { required: true, message: '审核人不能为空', trigger: 'blur' }
          ],
          auditType: [
            { required: true, message: '0-新房  1-二手房 2-出租房不能为空', trigger: 'blur' }
          ],
          relevanceId: [
            { required: true, message: '关联ID不能为空', trigger: 'blur' }
          ],
          auditStatus: [
            { required: true, message: '审核状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.auditId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.auditId) {
            this.$http({
              url: this.$http.adornUrl(`/order/audit/info/${this.dataForm.auditId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.auditName = data.audit.auditName
                this.dataForm.auditType = data.audit.auditType
                this.dataForm.relevanceId = data.audit.relevanceId
                this.dataForm.auditStatus = data.audit.auditStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/audit/${!this.dataForm.auditId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'auditId': this.dataForm.auditId || undefined,
                'auditName': this.dataForm.auditName,
                'auditType': this.dataForm.auditType,
                'relevanceId': this.dataForm.relevanceId,
                'auditStatus': this.dataForm.auditStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
